package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setVerifyCode(String phone, String code) {
        // 设置短信验证码key-value和过期时间
        // String key = "verify_code:" + phone;
        String key = RedisKeys.VERIFY_CODE.join(phone);
        // 参数：key，value，过期时间，时间单位
        redisTemplate.opsForValue().set(key,code, Consts.VERIFY_CODE_VAI_TIME * 60, TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
      //  String key = "verify_code:" + phone;
        String key = RedisKeys.VERIFY_CODE.join(phone);
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setToken(String token,UserInfo user) {
        String key= RedisKeys.USER_LOGIN_TOKEN.join(token);
        // 设置过期时间
        redisTemplate.opsForValue().set(key, JSON.toJSONString(user),Consts.USER_INFO_TOKEN_VAI_TIME * 60,TimeUnit.SECONDS);
    }

    @Override
    public UserInfo getToken(String token) {
        // 1、key，判断获取的token是否为空
        if (!StringUtils.hasLength(token)){
            return null;
        }
        String key = RedisKeys.USER_LOGIN_TOKEN.join(token);
        // 2、判断key存在，存在：延长有效时间
        if (redisTemplate.hasKey(key)){
            redisTemplate.expire(key,RedisKeys.USER_LOGIN_TOKEN.getTime(),TimeUnit.SECONDS);
            return JSON.parseObject(redisTemplate.opsForValue().get(key),UserInfo.class);  // 将String字符串 转为 UserInfo对象
        }
        return null;
    }

    @Override
    public String getAllAccess(String id) {
        String key = RedisKeys.User_All_Access.join(id);
        // 判断key是否存在，存在，直接返回，不存在，初始化一个key
        if(!redisTemplate.hasKey(key)){
            redisTemplate.opsForValue().set(key,"0");
        }
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public String getTodayAccess(String id) {
        String key = RedisKeys.User_Today_Access.join(id);
        // 判断key是否存在，存在，直接返回，不存在，初始化一个key
        if(!redisTemplate.hasKey(key)){
            Date date = new Date();
            Date endDate = DateUtil.getEndDate(date);
            long dateBetween = DateUtil.getDateBetween(date, endDate);
            redisTemplate.opsForValue().set(key,"0",dateBetween,TimeUnit.SECONDS);
        }
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void allNumIncrease(String id) {
        String key = RedisKeys.User_All_Access.join(id);
        redisTemplate.opsForValue().increment(key);
    }

    @Override
    public void todayNumIncrease(String id) {
        String key = RedisKeys.User_Today_Access.join(id);
        redisTemplate.opsForValue().increment(key);
    }
}
